<?php

class CheckoutIpnController extends BrowseControllerBase {
	
	public function __construct() {
		parent::__construct ();
	
	}
	
	/**
	 * Handle 2Checkout Return URL Notification
	 */
	public function indexAction() {
		$settingsDataService = new SettingsDataService ();
		$checkoutConfig = $settingsDataService->get2CheckoutSettings ();
		unset ( $checkoutConfig ['vendor_id'] );
		$gateway = new TwoCheckoutGateway ( $checkoutConfig );
		$gateway->setConfig ( $checkoutConfig );
		
		try {
			$ins = $this->getPost ();
			$gateway->getLog ()->log ( '\n' . ' 2CHECKOUT', zend_log::INFO );
			$gateway->getLog ()->log ( '\n ' . print_r ( $ins, true ), Zend_Log::INFO );
			$vendor_order_id = - 1;
			if (isset ( $ins ['merchant_order_id'] )) {
				$vendor_order_id = $ins ['merchant_order_id'];
			} else if (isset ( $ins ['vendor_order_id'] )) {
				$vendor_order_id = $ins ['vendor_order_id'];
			}
			
			$message_type = $ins ['message_type'];
			
			$md5_hash = $ins ['md5_hash'];
			$invoice_id = $ins ['invoice_id'];
			$vendor_id = $ins ['vendor_id'];
			$sale_id = $ins ['sale_id'];
			$secret = "tango";
			$md5_client = strtoupper ( md5 ( $sale_id . $vendor_id . $invoice_id . $secret ) );
			/*
			if($md5_client != $md5_hash){
				$error = "\n MD5 not match HASH1 = " . $md5_client . " HASH2 = " . $md5_hash;  
				$gateway->getLog()->log($error,zend_log::INFO);
				return ;
			}
			*/
			
			$orderDataService = new PackageOrderDataService ();
			$orderEntity = $orderDataService->getById ( $vendor_order_id );
			if ($orderEntity == null) {
				$gateway->getLog ()->log ( '\n' . ' not find orderEntity', zend_log::INFO );
				return;
			}
			$today = date ( 'Y-m-d' );
			$expiredDate = date ( 'Y-m-d', strtotime ( "$today + 1 months" ) );
			switch ($message_type) {
				case 'RECURRING_INSTALLMENT_SUCCESS' :
					$orderEntity->status = PackageOrderEntity::COMPLETED;
					$orderEntity->expiredDate = $expiredDate;
					$orderEntity->modifiedDate = date ( 'Y-m-d' );
					$orderDataService->update ( $orderEntity );
					$gateway->getLog ()->log ( '\n' . ' Payment SUCCESS BILLING, expired date will be extended to: ' . $expiredDate, zend_log::INFO );
					break;
				case 'RECURRING_INSTALLMENT_FAILED' :
					$orderEntity->status = PackageOrderEntity::CANCELLED;
					$orderEntity->modifiedDate = date ( 'Y-m-d' );
					$orderDataService->update ( $orderEntity );
					$gateway->getLog ()->log ( '\n' . ' Payment FAILED : ' . date ( 'Y-m-d' ), zend_log::INFO );
					break;
				case 'RECURRING_COMPLETE' :
					// Do nothing
					break;
				case 'RECURRING_STOPPED' :
					$orderEntity->status = PackageOrderEntity::CANCELLED;
					$orderEntity->modifiedDate = date ( 'Y-m-d' );
					$orderDataService->update ( $orderEntity );
					$gateway->getLog ()->log ( '\n' . ' Payment STOPPED : ' . date ( 'Y-m-d' ), zend_log::INFO );
					// Do nothing
					break;
				case 'RECURRING_RESTARTED' :
					break;
				case 'ORDER_CREATED' :
					$orderEntity->profileId = $sale_id;
					$orderDataService->update ( $orderEntity );
					break;
			}
		} catch ( Exception $ex ) {
			$error = "\n " . $ex->getMessage ();
			$gateway->getLog ()->log ( $error, Zend_Log::ERR );
		}
	}
}